﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using CoreShop.Model;
using CoreShop.Repository;
using SqlSugar;
namespace CoreShop.Services
{
    /// <summary>
    /// 接口实现
    /// </summary>
    public class corecmsscaleServices : Repository<corecmsscale>, IcorecmsscaleServices
    {
        public bool InsertAsync(corecmsscale entity)
        {
            return base.Change<corecmsscale>().Insert(entity);
        }
        public bool InsertOrUpdate(List<corecmsscale> entity)
        {
            return base.InsertOrUpdate(entity);
        }
        /// <summary>
        /// 重写异步更新方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(corecmsscale entity)
        {
            return base.Change<corecmsscale>().Update(a => new corecmsscale()
            {
                name = entity.name,
                ipAddress = entity.ipAddress,
            }, a => a.id == entity.id);
        }

        /// <summary>
        /// 重写异步更新方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(List<corecmsscale> entity)
        {
            bool returnKey = true;
            foreach (var item in entity)
            {
                returnKey = base.Change<corecmsscale>().Update(a => new corecmsscale()
                {
                    name = item.name,
                    ipAddress = item.ipAddress,
                }, a => a.id == item.id);
            }
            return returnKey;
        }

        /// <summary>
        /// 重写删除指定ID的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteByIdAsync(long id)
        {
            return base.Change<corecmsscale>().DeleteById(id);
        }

        /// <summary>
        /// 重写删除指定ID集合的数据(批量删除)
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DeleteByIdsAsync(int[] ids)
        {
            bool returnKey = true;
            foreach (int id in ids)
            {
                returnKey = base.Change<corecmsscale>().DeleteById(id);
            }
            return returnKey;
        }
        #region 获取缓存的所有数据==========================================================

        /// <summary>
        /// 获取缓存的所有数据
        /// </summary>
        /// <returns></returns>
        public List<corecmsscale> GetCaChe()
        {
            return base.GetList();
        }

        #endregion
    

        public List<corecmsscale> QueryAsync(long id)
        {
            List<corecmsscale> list = new List<corecmsscale>();
            var where = Expressionable.Create<corecmsscale>();
            if (id > 0)
            {
                where = where.And(a => a.id == id);
            }
            list = base.AsQueryable().WhereIF(true, where.ToExpression()).OrderBy(a => a.id).ToList();
            return list;
        }
            
    }
}
